import cloneDeep from 'lodash/cloneDeep'
import { hasOwnProperty } from './common'

/**
 * 仓库类
 */
export default class Store {
  private _store: StoreType = {}

  set(key: string, value: any) {
    this._store[key] = cloneDeep(value)
  }
  get<T = any>(key: string, defaultValue?: T): T {
    if (!this.has(key)) defaultValue
    return cloneDeep(this._store[key])
  }
  getAll() {
    return cloneDeep(this._store)
  }
  remove(key: string) {
    delete this._store[key]
  }
  has(key: string) {
    return hasOwnProperty(this._store, key)
  }
  clear() {
    this._store = {}
  }
}

type StoreType = {
  [k: string]: any
}
